﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_ReBuy : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            tbUserName.Text = _User.Name;
            //tbUserName.ReadOnly = true;

            btnReBuy.Attributes.Add("onclick", "return confirm('" + System.Web.HttpUtility.HtmlEncode("您确信输入的资料无误，并且立即补单吗？") + "');");
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/ReBuy.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnReBuy_Click(object sender, EventArgs e)
    {
        if (tbUserName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入消费ID。");
            return;
        }

        int UserType = int.Parse(ddlUserType.SelectedValue);
        double Money = 0;
        switch (UserType)
        {
            case 2:
                Money = PublicFunction.GetOptionsAsDouble("3单价格", 0);
                break;
            case 3:
                Money = PublicFunction.GetOptionsAsDouble("5单价格", 0);
                break;
            case 4:
                Money = PublicFunction.GetOptionsAsDouble("10单价格", 0);
                break; 
        }

        if (Money <= 0)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "购买金额错误", this.GetType().BaseType.FullName);

            return;
        }

        User tu = new User();
        tu.Name = ShoveEL._Convert.ToDBC(tbUserName.Text.Trim());
        string Return = "";
        int Result = tu.GetUserInfoByName(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (tu.UserType >= UserType)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "此消费ID的类型比您选择的类型更高或者一致，请选择一个更高的类型。");

            return;
        }

        Return = "";
        Result = _User.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);
            return;
        }

        Result = 0;
        Return = "";

        if (DAL.Procedures.P_ReBuy.Call(_User.ID, tu.ID, UserType, ref Result, ref Return) < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据库读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        Return = "";
        _User.GetUserInfoByID(ref Return);

        Panel1.Visible = false;
        Panel2.Visible = true;
    }
}